package com.xxx.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/8/31 10:47
 * @Copyright 博为峰
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private PersistentTokenRepository persistentTokenRepository;

    @Override
    protected UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }

    @Bean
    public PersistentTokenRepository getPersistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setCreateTableOnStartup(false);
        jdbcTokenRepository.setDataSource(dataSource);
        return jdbcTokenRepository;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //退出
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login").permitAll();

        http.exceptionHandling().accessDeniedPage("/unauth");//设置无权限访问跳转自定义页面

        http.formLogin().loginPage("/login")//自定义登陆页面
                .loginProcessingUrl("/logindo")//处理登陆路径
                .defaultSuccessUrl("/home").permitAll()//登陆成功访问的路径
                .and().authorizeRequests().antMatchers("/test02").permitAll() //设置不需要认证路径
//                .antMatchers("/test03").hasAuthority("test03")//权限
//                .antMatchers("/test03").hasAnyAuthority("admin,manager")//多个权限
//                .antMatchers("/test03").hasRole("admin")//角色
                .antMatchers("/test03").hasAnyRole("admin1,test")
                .and().rememberMe().tokenRepository(persistentTokenRepository).tokenValiditySeconds(60)//记住我
                .and().csrf().disable();//关闭csrf(跨站请求)防护
    }

    @Autowired
    public PasswordEncoder passwordEncoder;

//    @Override
//    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        final String encode = passwordEncoder.encode("123466");
//        auth.inMemoryAuthentication().withUser("root").password(encode).roles("admin");
//    }

    @Bean
    public PasswordEncoder getPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
